﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.ExtentionMethods;

namespace Happy.PessimisticLock
{
    /// <summary>
    /// 基于时间的过期策略。
    /// </summary>
    [Serializable]
    public class DateTimeExpirationPolicy : IExpirationPolicy
    {
        private readonly DateTime _start = DateTime.Now;
        private readonly TimeSpan _expiration;

        /// <summary>
        /// 构造方法。
        /// </summary>
        /// <param name="expiration">过期时间间隔</param>
        public DateTimeExpirationPolicy(TimeSpan expiration)
        {
            _expiration = expiration;
        }

        /// <summary>
        /// 构造方法。
        /// </summary>
        /// <param name="minute">过期的分钟</param>
        public DateTimeExpirationPolicy(double minute)
        {
            minute.MustGreaterThan("minute", 0);

            _expiration = TimeSpan.FromMinutes(minute);
        }

        /// <summary>
        /// 是否过期。
        /// </summary>
        public bool IsExpired()
        {
            return (DateTime.Now - _start) > _expiration;
        }
    }
}
